package com.lh.controller;

import com.lh.enums.CategoryType;
import com.lh.enums.YesOrNo;
import com.lh.pojo.Carousel;
import com.lh.pojo.Category;
import com.lh.pojo.vo.CategoryVO;
import com.lh.pojo.vo.ItemVO;
import com.lh.service.CarouselService;
import com.lh.service.CategoryService;
import com.lh.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-04-28 15:06
 **/
@Api(value = "首页相关接口", tags = {"首页相关api接口"})
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "轮播图获取", notes = "轮播图获取", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/carousel")
    public IMOOCJSONResult getCarousel(){
        List<Carousel> carouselList = carouselService.getCarousel(YesOrNo.YES.getType());
        return IMOOCJSONResult.ok(carouselList);
    }

    @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/cats")
    public IMOOCJSONResult getCategory(){
        List<Category> categoryList = categoryService.getCategory(CategoryType.ONE.getType());
        return IMOOCJSONResult.ok(categoryList);
    }

    @ApiOperation(value = "获取商品分类(子分类)", notes = "获取商品分类(子级分类)", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/subCat/{rootCatId}")
    public IMOOCJSONResult getSubCategory(
            @ApiParam(name = "商品分类等级Id", value = "商品分类等级Id", required = true)
            @PathVariable Integer rootCatId){

        if (rootCatId == null){
            IMOOCJSONResult.errorMsg("商品分类等级Id为空!");
        }

        List<CategoryVO> categoryVOList = categoryService.getSubCategory(rootCatId);
        return IMOOCJSONResult.ok(categoryVOList);
    }

    @ApiOperation(value = "查询每个一级分类下的最新六条商品数据", notes = "查询每个一级分类下的最新六条商品数据", httpMethod = "GET",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/sixNewItems/{rootCatId}")
    public IMOOCJSONResult getSixNewItems(
            @ApiParam(name = "商品分类等级Id", value = "商品分类等级Id", required = true)
            @PathVariable Integer rootCatId){

        if (rootCatId == null){
            IMOOCJSONResult.errorMsg("商品分类等级Id为空!");
        }

        Map<String,Integer> map = new HashMap<>();
        map.put("rootCatId", rootCatId);

        List<ItemVO> itemVOList = categoryService.getSixNewItems(map);

        return IMOOCJSONResult.ok(itemVOList);
    }


}
